import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:x_project/res/colors.dart';

/// Created by XieXin on 2021/8/4.
/// 单选对话框
class SingleChoiceDialog {
  static void showSingleChoiceDialog(
    context, {
    String title = "请选择",
    int height = 500,
    required List<String> list,
    Function(int index)? onClickItem,
  }) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return Material(
          type: MaterialType.transparency, //透明类型
          child: WillPopScope(
              //禁用返回键
              child: Center(
                child: Container(
                  margin: EdgeInsets.all(ScreenUtil().setWidth(50)),
                  width: double.infinity,
                  height: ScreenUtil().setHeight(height),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20.w),
                  ),
                  child: SingleChoiceWidget(
                    title: title,
                    list: list,
                    onClickItem: onClickItem,
                  ),
                ),
              ),
              onWillPop: () async {
                return Future.value(false);
              }),
        );
      },
    );
  }
}

class SingleChoiceWidget extends StatefulWidget {
  final String title;
  final List<String> list;
  final Function(int index)? onClickItem;

  const SingleChoiceWidget(
      {required this.title, required this.list, this.onClickItem, Key? key})
      : super(key: key);

  @override
  SingleChoiceWidgetState createState() => SingleChoiceWidgetState();
}

class SingleChoiceWidgetState extends State<SingleChoiceWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Column(
        children: [
          SizedBox(
            width: double.infinity,
            height: ScreenUtil().setWidth(92),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Text(
                  widget.title,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(36),
                    color: XColors.textColor33,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Positioned(
                  right: ScreenUtil().setWidth(12),
                  child: InkWell(
                    onTap: () => Navigator.of(context).pop(),
                    child: Container(
                      width: ScreenUtil().setWidth(80),
                      height: ScreenUtil().setWidth(80),
                      padding: EdgeInsets.symmetric(vertical: 22.w),
                      child: Icon(
                        const IconData(0xe889, fontFamily: "Icons"),
                        size: 42.w,
                        color: XColors.textColorB1,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.only(bottom: ScreenUtil().setWidth(20)),
              child: ListView.builder(
                physics: const BouncingScrollPhysics(),
                itemCount: widget.list.length,
                itemBuilder: (context, index) {
                  return _getItemView(index);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _getItemView(int index) {
    return GestureDetector(
      onTap: () {
        Navigator.pop(context);
        if (widget.onClickItem != null) widget.onClickItem!(index);
      },
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: 32.w,
          vertical: 16.w,
        ),
        color: index % 2 == 0 ? Colors.grey[50] : Colors.white,
        width: double.infinity,
        child: Text(
          widget.list[index],
          style: const TextStyle(color: XColors.textColor66),
        ),
      ),
    );
  }
}
